package com.bnzj.cloud.business.core.web;

import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import com.bnzj.cloud.business.core.persistence.entity.SysSetting;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bnzj.cloud.business.core.service.SysSettingService;
import com.bnzj.core.action.ActionType;
import com.bnzj.core.action.annotation.Action;
import com.bnzj.core.exception.BusinessException;
import com.bnzj.core.persistence.query.CompareExpression;
import com.bnzj.core.persistence.query.CompareType;
import com.bnzj.core.persistence.query.IExpression;
import com.bnzj.core.persistence.query.QueryHelpBean;
import com.bnzj.core.persistence.util.EntityUtils;
import com.bnzj.core.rest.CurrentUserService;
import com.bnzj.core.rest.ResponseResult;
import com.bnzj.core.rest.ResultCode;
import com.bnzj.core.rest.UserInfo;
import com.bnzj.core.util.BeanUtils;
import com.bnzj.core.webmvc.util.WebMvcUtils;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * @author Barry
 * @createDate 2020/2/17
 * @description
 */
@Tag(name = "系统配置", description = "系统配置")
@RestController
@RequestMapping("/system/setting")
public class SysSettingController {

    @Autowired
    private CurrentUserService gatewayCurrentUserService;

    @Autowired
    private SysSettingService sysSettingService;


    @Operation(summary = "分页查询")
    @GetMapping(value = "/page")
    public ResponseResult<Page<SysSetting>> getSettingPage(HttpServletRequest request, @Parameter(description = "名称") @RequestParam(required = false) String name) {
        Pageable pageable = WebMvcUtils.initPage(request, Sort.by(Sort.Direction.DESC, "modify_time"));
        QueryHelpBean bean = SysSettingService.getQueryHelpBean();
        bean.setPageable(pageable);
        //配置名称模糊搜索
        IExpression orCondition1 = CompareExpression.build("name", CompareType.like, name);
        bean.addWhere(orCondition1);
        Page<SysSetting> all = sysSettingService.findAll(bean);
        return ResponseResult.success(all);
    }


    @Operation(summary = "根据id查询")
    @GetMapping("/one/{id}")
    public ResponseResult<SysSetting> getById(@Parameter(in = ParameterIn.PATH, name = "id", description = "主键", required = true) @PathVariable("id") Long id) {
        Optional<SysSetting> optional = sysSettingService.findById(id);
        return ResponseResult.success(optional.orElse(null));
    }


    @Operation(summary = "保存")
    @Action(descriprion = "系统配置",moduleName = "系统配置",operateType = ActionType.ADD_DATA)
    @PostMapping("save")
    public ResponseResult<?> addSetting(@RequestBody SysSetting sysDictionary) {
        UserInfo userInfo = gatewayCurrentUserService.getUserInfo();
        EntityUtils.saveBefore(sysDictionary, userInfo.getAccount(), userInfo.getName());
        sysSettingService.save(sysDictionary);
        return ResponseResult.success();
    }


    @Operation(summary = "更新")
    @Action(descriprion = "系统配置更新",moduleName = "系统配置",operateType = ActionType.UPDATE_DATA)
    @PostMapping("/update")
    public ResponseResult<?> updateSetting(@RequestBody SysSetting sysSetting) {
        if (ObjectUtils.isEmpty(sysSetting.getId())) {
            throw new BusinessException(ResultCode.getResultCode(ResultCode.BAD_REQUEST));
        }
        UserInfo userInfo = gatewayCurrentUserService.getUserInfo();
        Optional<SysSetting> sysSettingOpt = sysSettingService.findById(sysSetting.getId());
        if (!sysSettingOpt.isPresent()) {
            throw new BusinessException(ResultCode.getResultCode(ResultCode.RESOURCE_NOT_EXIST));
        }
        SysSetting sysSettingDb = sysSettingOpt.get();
        BeanUtils.copyProperties(sysSetting, sysSettingDb);
        EntityUtils.saveBefore(sysSettingDb, userInfo.getAccount(), userInfo.getName());
        sysSettingService.save(sysSettingDb);
        return ResponseResult.success();
    }
}
